﻿using Management.API.Application.CommandHandler.Management;
using Management.API.Application.CommandHandler.Management.Product;
using Management.API.Application.Controller.Management;
using Management.API.Application.Controller.Management.Material;
using Management.API.Application.Controller.Management.Product;
using Management.API.DTOs.Management;
using Management.Errorcode;
using Management.Models.BOMBasicInformation;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Management.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ProductController : ControllerBase
    {
        private readonly ILogger<ProductController> _logger;
        private readonly IMediator _mediator;
        public ProductController(ILogger<ProductController> logger, IMediator mediator)
        {
            _logger = logger;
            _mediator = mediator;
        }


        /// <summary>
        /// 获取产品类型信息
        /// </summary>  
        /// <returns>所有产品类型信息</returns>
        [HttpGet]
        public async Task<ApiResult<IList<GetProductTypeDTO>>> GetAllProductTypes([FromQuery]GetProductTypeCommand query)
        {
            // 创建一个没有查询条件的查询对象
            //var query = new GetProductTypeCommand();
            return await _mediator.Send(query);
        }


        /// <summary>
        /// 获取产品分类信息
        /// </summary>  
        /// <returns>所有产品分类信息</returns>
        [HttpGet]
        public async Task<ApiResult<IList<GetProductClassificationDTO>>> GetAllProductCategories([FromQuery] GetProductClassificationCommand query)
        {
            // 创建一个没有查询条件的查询对象
            //var query = new GetProductClassificationCommand();
            return await _mediator.Send(query);
        }


        /// <summary>
        /// 获取产品属性信息
        /// </summary>            
        /// <returns>所有产品属性信息</returns>
        [HttpGet]
        public async Task<ApiResult<IList<GetProductAttributeDTO>>> GetAllProductProperties([FromQuery] GetProductAttributeCommand query)
        {
            // 创建一个没有查询条件的查询对象
           // var query = new GetProductAttributeCommand();
            return await _mediator.Send(query);
        }


        /// <summary>
        /// 获取产品信息
        /// </summary> GetProductCommandHandler
        /// <returns>所有产品信息</returns>
        [HttpGet]
        public async Task<ApiResult<IList<GetProductDTO>>> GetAllProducts([FromQuery] GetProductCommand query)
        {
            // 创建一个没有查询条件的查询对象
            //var query = new GetProductCommand();
            return await _mediator.Send(query);
        }


        /// <summary>
        /// 添加产品信息
        /// </summary>
        /// <param name="command">添加产品的请求命令</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<ApiResult> AddProduct([FromBody] AddProductCommand command)
        {
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 删除产品信息
        /// </summary>
        /// <param name="productId">要删除的产品ID</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<ApiResult> DeleteProduct(int productId)
        {
            var command = new DeleteProductCommand { ProductId = productId };
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 根据产品ID获取产品信息
        /// </summary>
        /// <param name="productId">产品的唯一标识符</param>
        /// <returns>产品信息</returns>
        [HttpGet]
        public async Task<ApiResult> GetProductById(int productId)
        {
            var query = new GetProductCommand { ProductId = productId };
            return await _mediator.Send(query);
        }
        /// <summary>
        /// 修改产品信息
        /// </summary>
        /// <param name="command">修改产品的请求命令</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<ApiResult> UpdateProduct([FromBody] UpdateProductCommand command)
        {
            return await _mediator.Send(command);
        }















    }
}
